#!/bin/bash

usage()
{
	echo "Usage: ./ostest nmap-os-db ifname ip-addr"
	echo -e "\tnmap-os-db = Path to the nmap-os-db file that you wish to test against"
	echo -e "\tifname = The network interface on which to do the testing"
	echo -e "\tip-addr = The IP address to put the honeyd VM on"
}

printout()
{
	echo "Exiting now. Cleaning up honeyd and nmap processes..."
	killall honeyd
	killall nmap
	
	echo -e "\nTotal $count Lines read"
	echo Total successful personalities: $WIN
	echo Total failed personalities: $LOSE

	exit
}

trap printout SIGINT
trap printout KILL
trap printout TERM

#Assert root user
if [ $USER != "root" ]; then
	echo "Execute with root privileges: sudo $0"
	exit 1
fi

if [ -z $1 ]; then
	usage
	exit 1
else
	FILENAME=$1
fi

if [ -z $2 ]; then
	usage
	exit 1
else
	IFNAME=$2
fi

if [ -z $3 ]; then
	usage
	exit 1
else
	VMIP=$3
fi


rm -f result
mkdir -p results

TOTALFINGERPRINTS=0
count=0
WIN=0
LOSE=0
PERSONALITY=""

# Figure out total count for status so far/percent complete
while read LINE ; do
	#Only consider Fingerprint lines
	if [[ "$LINE" == Fingerprint* ]]
	then
		((TOTALFINGERPRINTS++))
	fi
done < $FILENAME

while read LINE ; do

	#Only consider Fingerprint lines
	if [[ "$LINE" == Fingerprint* ]]
	then
		((count++))

		#Chop off the "Fingerprint " prefix
		LINE=${LINE:12}
		PERSONALITY=$LINE
		echo "Testing personality $count/$TOTALFINGERPRINTS ($((100*$count/$TOTALFINGERPRINTS))%) : $PERSONALITY"

		CONFIG="
create default
set default default tcp action filtered
set default default udp action filtered
set default default icmp action closed

create test\n
set test personality \"$LINE\"\n
set test uptime 1728650\n
set test maxfds 35\n
add test tcp port 80 open\n
add test udp port 139 open\n
set test ethernet \"dell\"\n
set test default tcp action closed\n
set test default udp action closed\n
set test default icmp action closed\n\n
bind \"$VMIP\" test\n"

		configFile=$(pwd)
		configFile="$configFile/test.config"

		echo -e $CONFIG > test.config
		#cat test.config
	
		echo -e "START PERSONALITY $PERSONALITY \n" >> results/honeyd.txt
		honeyd -p $FILENAME -i $IFNAME -f $configFile &>> results/honeyd.txt
		echo -e "STOP PERSONALITY $PERSONALITY \n" >> results/honeyd.txt

		sleep 2
			
		nmap -F -O --fuzzy $VMIP &> "results/nmap.temp"

		killall honeyd

		#cat nmap.out

		#compare results!
		found="false"
		while read INNERLINE ; do
			if [[ "$INNERLINE" == "Aggressive OS guesses: "* ]]; then
				found=true
				# Cut off everything past the first comma if there is one
				PERC=$(expr index "$INNERLINE" "'%'")
				if [[ $PERC != 0 ]]; then
					PERCENTMATCH=${INNERLINE:$(expr $PERC - 3):3}
					INNERLINE=${INNERLINE:0:$(expr $PERC - 5)}
				fi

				INNERLINE=${INNERLINE:23}
				#echo "INNERLINE IS '$INNERLINE'"
				if [[ $INNERLINE == $LINE ]]; then
					echo SUCCESS ${INNERLINE:$END:2} $LINE >> result
					WIN=$((WIN+1))
					found="Correct Partial Match $PERCENTMATCH"
				else
					echo FAIL $LINE >> result
					LOSE=$((LOSE+1))
					found="Incorrect Partial Match"
				fi
				
				
			fi
			if [[ "$INNERLINE" == "OS details: "* ]]; then
				if [[ $INNERLINE == *"$LINE"* ]]; then
					echo SUCCESS 100 $LINE >> result
					WIN=$((WIN+1))
					found="Correct Exact Match"
				else
					echo FAIL $LINE >> result
					LOSE=$((LOSE+1))
					found="Incorrect Exact Match"
				fi
			fi
			

		done < "results/nmap.temp"

		# Aggregate our nmap output for later analysis
		echo -e "START PERSONALITY $PERSONALITY \n" >> results/nmap.txt
		cat results/nmap.temp >> results/nmap.txt
		echo -e "STOP PERSONALITY $PERSONALITY \n" >> results/nmap.txt

		if [[ $found == "false" ]]; then
			echo -e "FAILED! No OS lines found in nmap output. \n"
		else
			echo -e "$found \n"
		fi
	fi

done < $FILENAME

printout
